package top.infopub.zookeeper.confclient.config;


import java.util.HashMap;
import java.util.Properties;

import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.util.Assert;

import top.infopub.exception.ServiceException;


/**
 * 系统参数配置工厂类，实现了spring FactoryBean接口
 * </br>
 * 在代码中需要读取配置信息时，调用方法为：PropertiesConfigurationFactoryBean.getPropertiesConfiguration();
 */
public class PropertiesConfigurationFactoryBean implements FactoryBean<Properties> {

    private CuratorFramework zkclient;

    private static PropertiesConfiguration __configuration;

    private static boolean init = false;

    public PropertiesConfigurationFactoryBean(CuratorFramework zkclient) {
        __configuration = new PropertiesConfiguration(zkclient);
        init = true;
    }

    public PropertiesConfigurationFactoryBean(CuratorFramework zkclient,
                                              HashMap<String, String> listener) {
        __configuration = new PropertiesConfiguration(zkclient, listener);
        init = true;
    }

    public static PropertiesConfiguration getPropertiesConfiguration() {
        if (!init) {
            throw new ServiceException("PropertiesConfigurationFactoryBean 没有初始化");
        }
        return __configuration;
    }
    
    @Override
    public Properties getObject()
        throws Exception {
        Assert.notNull(__configuration);
        return __configuration.getProperties();
    }

    @Override
    public Class<?> getObjectType() {
        return Properties.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public PropertiesConfiguration get__configuration() {
        return __configuration;
    }

    public CuratorFramework getZkclient() {
        return zkclient;
    }

    public void setZkclient(CuratorFramework zkclient) {
        this.zkclient = zkclient;
    }
}
